//
//  ShareDanCell.swift
//  HappyEat
//
//  Created by ctx2016 on 17/1/11.
//  Copyright © 2017年 陌路是我. All rights reserved.
//

import UIKit

enum ShareDanType {
    case shareDanType
    case mineShareType
}

class ShareDanCell:UITableViewCell ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    var heaserImage:UIImageView = UIImageView.init()
    var qhLabel:UILabel = UILabel.init()
    var userNameLabel:UILabel = UILabel.init()
    var dateLabel:UILabel = UILabel.init()
    var infoLabel:UILabel = UILabel.init()
    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
    var collectionView:UICollectionView?
    var typeShare:ShareDanType? {
        didSet{
           self.setUI()
        }
    }
    var dataDic:[String:String]?{
        didSet{
            if self.typeShare == .shareDanType{
                self.heaserImage.snp.makeConstraints { (make) in
                    make.left.equalTo(16)
                    make.top.equalTo(12)
                    make.size.equalTo(CGSize.init(width: 45, height: 45))
                }
                self.qhLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(self.heaserImage.snp.right).offset(12)
                    make.height.equalTo(14)
                    make.right.equalTo(-16)
                    make.top.equalTo(12)
                }
                let width:CGFloat = self.userNameLabel.width(forText: self.userNameLabel.text ?? "", withFontSize: 14, withTextHeight: 14)
                self.userNameLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(self.heaserImage.snp.right).offset(14)
                    make.width.equalTo(width)
                    make.top.equalTo(self.qhLabel.snp.bottom).offset(12)
                    make.height.equalTo(14)
                }
                self.dateLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(self.userNameLabel.snp.right).offset(0)
                    make.right.equalTo(-16)
                    make.top.equalTo(self.userNameLabel)
                    make.height.equalTo(14)
                }
                let height:CGFloat = self.infoLabel.jqGetHeightWithstr(self.infoLabel.text ?? "", withfontSize: 14, withLineSpace: 0, withmaxWidth: k_Wide - 32)
                //        if height > 28.0 {
                //            height = 28.0
                //        }
                self.infoLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(16)
                    make.right.equalTo(-16)
                    make.height.equalTo(height)
                    make.top.equalTo(self.heaserImage.snp.bottom).offset(12)
                }
                self.collectionView?.snp.makeConstraints { (make) in
                    make.top.equalTo(self.infoLabel.snp.bottom).offset(8)
                    make.left.equalTo(16)
                    make.right.equalTo(-16)
                    make.height.equalTo(75)
                }
            }else{
//                self.heaserImage.snp.makeConstraints { (make) in
//                    make.left.equalTo(16)
//                    make.top.equalTo(12)
//                    make.size.equalTo(CGSize.init(width: 45, height: 45))
//                }
                self.qhLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(16)
                    make.height.equalTo(14)
                    make.right.equalTo(-16)
                    make.top.equalTo(12)
                }
                let width:CGFloat = self.userNameLabel.width(forText: self.userNameLabel.text ?? "", withFontSize: 14, withTextHeight: 14)
                self.userNameLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(16)
                    make.width.equalTo(width)
                    make.top.equalTo(self.qhLabel.snp.bottom).offset(12)
                    make.height.equalTo(14)
                }
                self.dateLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(self.userNameLabel.snp.right).offset(0)
                    make.right.equalTo(-16)
                    make.top.equalTo(self.userNameLabel)
                    make.height.equalTo(14)
                }
                let height:CGFloat = self.infoLabel.jqGetHeightWithstr(self.infoLabel.text ?? "", withfontSize: 14, withLineSpace: 0, withmaxWidth: k_Wide - 32)
                //        if height > 28.0 {
                //            height = 28.0
                //        }
                self.infoLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(16)
                    make.right.equalTo(-16)
                    make.height.equalTo(height)
                    make.top.equalTo(self.userNameLabel.snp.bottom).offset(12)
                }
                self.collectionView?.snp.makeConstraints { (make) in
                    make.top.equalTo(self.infoLabel.snp.bottom).offset(8)
                    make.left.equalTo(16)
                    make.right.equalTo(-16)
                    make.height.equalTo(75)
                }
            }
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
//    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
//        super.init(style: style, reuseIdentifier: reuseIdentifier)
//        self.setUI()
//    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func layoutSubviews() {
    }
}
extension ShareDanCell {
    func setUI(){
        if self.typeShare == .shareDanType{
            self.layout.itemSize = CGSize.init(width: k_Wide/4, height: 75)
            self.layout.minimumInteritemSpacing = 10
            self.collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: self.layout)
            self.contentView.addSubview(self.heaserImage)
            self.contentView.addSubview(self.qhLabel)
            self.contentView.addSubview(self.userNameLabel)
            self.contentView.addSubview(self.dateLabel)
            self.contentView.addSubview(self.infoLabel)
            self.contentView.addSubview(self.collectionView!)
            self.collectionView?.delegate = self
            self.collectionView?.dataSource = self
            self.collectionView?.backgroundColor = .white
            self.collectionView?.register(ShareDanCollectionCell.self , forCellWithReuseIdentifier: "CellId")
            self.dateLabel.textAlignment = .right
            self.dateLabel.textColor = .lightGray
            self.dateLabel.font = UIFont.systemFont(ofSize: 12)
            self.heaserImage.layer.cornerRadius = 22.5
            self.heaserImage.backgroundColor = .black
            self.qhLabel.font = UIFont.systemFont(ofSize: 14)
            self.userNameLabel.font = UIFont.systemFont(ofSize: 14)
            self.userNameLabel.textColor = .blue
            self.infoLabel.numberOfLines = 0
            self.infoLabel.textColor = .lightGray
        }else{
            self.layout.itemSize = CGSize.init(width: k_Wide/4, height: 75)
            self.layout.minimumInteritemSpacing = 10
            self.collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: self.layout)
//            self.contentView.addSubview(self.heaserImage)
            self.contentView.addSubview(self.qhLabel)
            self.contentView.addSubview(self.userNameLabel)
            self.contentView.addSubview(self.dateLabel)
            self.contentView.addSubview(self.infoLabel)
            self.contentView.addSubview(self.collectionView!)
            self.collectionView?.delegate = self
            self.collectionView?.dataSource = self
            self.collectionView?.backgroundColor = .white
            self.collectionView?.register(ShareDanCollectionCell.self , forCellWithReuseIdentifier: "CellId")
            self.dateLabel.textAlignment = .right
            self.dateLabel.textColor = .lightGray
            self.dateLabel.font = UIFont.systemFont(ofSize: 12)
            self.heaserImage.layer.cornerRadius = 22.5
            self.heaserImage.backgroundColor = .black
            self.qhLabel.font = UIFont.systemFont(ofSize: 14)
            self.userNameLabel.font = UIFont.systemFont(ofSize: 14)
            self.userNameLabel.textColor = .blue
            self.infoLabel.numberOfLines = 0
            self.infoLabel.textColor = .lightGray
        }
    }
    //MARK:CollectionviewDelegate
    @objc(collectionView:didSelectItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("选中事件")
    }
    @objc(numberOfSectionsInCollectionView:) func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    @objc(collectionView:cellForItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellId", for: indexPath)
//        cell.backgroundColor = .black
        return cell
    }
}
